#include <QSqlTableModel>
#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
#include <QHostAddress>

#include "linklistform.h"
#include "database.h"


LinkListForm::LinkListForm(const QStringList & fileNames)
{
    setupUi( this );

    linkTableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem(tr("begin")));
    linkTableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem(tr("end")));
    linkTableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem(tr("average time")));
    linkTableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem(tr("count")));

    for(int i = 0; i < fileNames .size(); ++i)
        showLinkList(DB.getLinkList(fileNames.at(i)));

    for(int i = 0; i < 4; ++i)
        linkTableWidget->resizeColumnToContents(i);
}

LinkListForm::~LinkListForm()
{

}


void LinkListForm::showLinkList(const LinkList& linkList)
{
    int row;
    for(int i = 0; i < linkList.size(); ++i){
        row = linkTableWidget->rowCount();
        linkTableWidget->insertRow(row);
#if 0
        qDebug() << QHostAddress(linkList.at(i).begin).toString()\
                 << QHostAddress(linkList.at(i).end).toString()\
                 << QString::number(linkList.at(i).averageTime)\
                 << QString::number(linkList.at(i).nodeCount);
#endif
        linkTableWidget->setItem(row, 0, new QTableWidgetItem(QHostAddress(linkList.at(i).begin).toString()));
        linkTableWidget->setItem(row, 1, new QTableWidgetItem(QHostAddress(linkList.at(i).end).toString()));
        linkTableWidget->setItem(row, 2, new QTableWidgetItem(QString::number(linkList.at(i).averageTime)));
        linkTableWidget->setItem(row, 3, new QTableWidgetItem(QString::number(linkList.at(i).nodeCount)));
    }
}
